package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.user.UserShoppingCartService;
import com.sky.utils.GetHttp;
import com.sky.utils.ParseJWTGetId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@RequiredArgsConstructor
public class UserShoppingCartController {
    private final UserShoppingCartService shoppingCartService;
    private final ParseJWTGetId parseJWTGetId;
    private final GetHttp getHttp;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        ShoppingCart shoppingCart = new ShoppingCart();
        log.info("添加ShoppingCartDTO:{}", shoppingCartDTO);
        shoppingCartService.add(shoppingCart,shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> findAll(){
        Integer userId = parseJWTGetId.getUserId(getHttp.getCurrentHttpServletRequest());
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll(userId);
        return Result.success(shoppingCartList);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("sub")
    public Result<Object> deleteById(@RequestBody ShoppingCartDTO shoppingCartDTO){
        Integer userId = parseJWTGetId.getUserId(getHttp.getCurrentHttpServletRequest());
        shoppingCartService.deleteById(shoppingCartDTO,userId);
        return Result.success();
    }

    @DeleteMapping("/clean")
    public Result<Object> deletesById(){
        Integer userId = parseJWTGetId.getUserId(getHttp.getCurrentHttpServletRequest());
        shoppingCartService.deletesById(userId);
        return Result.success();
    }
}
